﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using INHSStudentRegistration;

namespace IrisanNHS
{
    public partial class Form6 : Form
    {
        MainForm parent;

        DataBaseConnection DBC = new DataBaseConnection();
        UserAccount UAccount = new UserAccount();
        public int InDeX;
        DataTable DT = new DataTable();

        public Form6(MainForm p)
        {
            InitializeComponent();
            parent = p;
            this.Load += new EventHandler(Form6_Load);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);
            dataGridView.Click += new EventHandler(dataGridView_Click);
            btnDelete.Click += new EventHandler(btnDelete_Click);
            btnupdate.Click += new EventHandler(btnupdate_Click);
        }

        private void deleteuser()
        {
            UAccount.uaname = txtname.Text;
            UAccount.uauname = txtusername.Text;
            UAccount.uapassword = txtpassword.Text;
            UAccount.lvlofaccess = comboBoxlvlofaccess.Text;
            
            DBC.UserAccount(UAccount);
            MessageBox.Show("DELETED!");
        }

        void btnupdate_Click(object sender, EventArgs e)
        {
            if (txtusername.Text == "" || txtpassword.Text == "")
            {
                MessageBox.Show("INVALID");
                return;
            }

            else
            {
                DBC.Update_UserAccount(UAccount, InDeX);
                MessageBox.Show("UPDATE SUCCESSFUL!");

            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            //deleteuser();
            dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);
        }

        void dataGridView_Click(object sender, EventArgs e)
        {
            
                InDeX = dataGridView.SelectedRows[0].Index;
                if (dataGridView.Rows.Count != 0)
                {

                    txtname.Text = DBC.DT.Rows[InDeX][0].ToString();
                    txtusername.Text = DBC.DT.Rows[InDeX][1].ToString();
                    txtpassword.Text = DBC.DT.Rows[InDeX][2].ToString();
                    comboBoxlvlofaccess.Text = DBC.DT.Rows[InDeX][3].ToString();

                }
            
        }

        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        void Form6_Load(object sender, EventArgs e)
        {
            DBC.DBConnection();
            DBC.MyQuery("SELECT * FROM accounttable");
            DT = DBC.DT;
            dataGridView.DataSource = DT;
            DBC.Close();
        }

        private void Search()
        {
            string mysql = "SELECT * FROM accounttable WHERE Like '%" + txtSearch + "%'";
            DBC.MyQuery(mysql);
            DT = DBC.DT;
            dataGridView.DataSource = DT;
        }
    }
}
